﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

// Added
using System.IO;
using System.Xml;
using GELib;

namespace GameEditor
{
    /// <summary>
    /// Interaction logic for dlg_Options.xaml
    /// </summary>
    public partial class dlg_Options : Window
    {


        public dlg_Options()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            loadGeneralTab();
            load2ndTab(); // etc.
        }

        private void loadGeneralTab()
        {
            LangManager.langLoaded += new EventHandler(changeLangTexts);
            changeLangTexts(null, EventArgs.Empty);

            DirectoryInfo langDir = new DirectoryInfo(GEConfig.langPath);
            XmlDocument xmlDoc = new XmlDocument();

            // Get lang files in lang directory
            FileInfo[] langFiles = langDir.GetFiles();

            // fill combobox with lang files' names and select default language.
            String[] LangSplit = GEConfig.defaultLanguage.Split('/');
            String DefaultLangFileName = LangSplit[LangSplit.Count() - 1];

            foreach (FileInfo fi in langFiles)
            {
                if (fi.Extension == ".xml")
                {
                    // open xml file to get langue name
                    xmlDoc.Load(GEConfig.langPath + fi.Name);
                    String title = xmlDoc.GetElementsByTagName("lang")[0].InnerText;

                    //Create combobox item
                    ComboBoxItem cboi = new ComboBoxItem();
                    cboi.Content = title;
                    cboi.Tag = GEConfig.langPath + fi.Name;
                    cboLang.Items.Add(cboi);

                    // if "fi" is default language file, then select it
                    if (DefaultLangFileName == fi.Name)
                        cboLang.SelectedItem = cboi;
                }
            }
        }
        private void load2ndTab() { }
        // etc.

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void cboLang_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBoxItem cboi = (ComboBoxItem)cboLang.SelectedItem;
            if (cboi != null)
            {
                LangManager.loadLanguage(cboi.Tag.ToString());

                // Change defaultLanguage in GEConfig.xml
                GEConfig.changeField("DefaultLanguage", cboi.Tag.ToString());
            }
        }


        void changeLangTexts(object sender, EventArgs e)
        {
            this.Title = LangManager.getPhrase("opt_Title");
            tiGeneral.Header = LangManager.getPhrase("opt_General_Tab");
            txbLang.Text = LangManager.getPhrase("opt_General_Tab_LangLabel");
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            // updates config infos in GEConfig
            GEConfig.loadConfig();
        }
    }
}
